<template>
	<view class="login-login-contain">
		<view class="login-logo">
			<image class="login-img" :src="programLogo" mode="widthFix"></image>
			<text class="login-logo-text">{{programName}}</text>
		</view>
		<view class="login-opt">
			<button open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber" class="login-opt-btn" type="default">微信登录</button>
			<text @click="toTelLogin" class="login-opt-text">手机号登录/注册</text>
		</view>

		<view class="login-pro-con">
			<text class="login-pro-text">登录代表您已同意</text>
			<text class="login-pro-pro" @click="gotoAgreement">{{programName}}平台会员服务协议</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		loginUrl,
		phoneUrl
	} from '@/utils/index.js';
	import {
		programName,
		programLogo
	} from '@/common/config'
	export default {
		data() {
			return {
				isShowAuth: true,
				sessionKey: '',
				isAuth: false,
				encrypdata: '',
				ivdata: '',
				openid: null,
				errorLonginFlag: false, //异常原因 重新登录表示
				programName: programName,
				programLogo:programLogo
			}
		},
		onLoad(option) {
			uni.removeStorageSync('userInfo');
			this.getWxCode();
			if (option.phoneNumber != 'undefined' && option.phoneNumber != undefined) {
				this.getLogoutTopic(option.phoneNumber)
			}

		},
		onUnload() {
			this.errorLonginFlag = false;
			uni.removeStorageSync('autoGoLoginFlag');
		},
		computed: {
			...mapState('userStore', ['hasLogin', 'userInfo']),
		},
		methods: {
			...mapMutations('userStore', ['saveUserInfo', 'SetIsSubscribe','setToken','setPermList','setMainAccount','clearPermList']),
			gotoAgreement() {
				uni.navigateTo({
					url: '/pagesA/agreement/agreement'
				});
			},
			toTelLogin() {
				uni.navigateTo({
					url: '/pagesA/tel-login/tel-login'
				});
			},
			//获取掉线原因
			getLogoutTopic(v) {
				this.$request.get({
					url: 'user/getLogoutTopic',
					data: {
						phoneNumber: v
					},
					force: false
				}).then(res => {
					this.errorLonginFlag = true
					uni.showToast({
						title: res,
						duration: 2000,
						icon: 'none'
					})
				})
			},
			//获取微信code
			getWxCode() {
				let that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log('获取得code',loginRes.code)
						that.wxLogin(loginRes.code)
					}
				});
			},
			//将code传给后台
			wxLogin(code) {
				let that = this;
				this.$request.get({
					url: loginUrl,
					data: {
						code: code
					},
				}).then(res => {
					this.sessionKey = res.sessionKey;
					const openid = res.openid;
					this.openid = openid;
					uni.setStorageSync("openid", this.openid);
				})
			},
			//获取用户手机号
			decryptPhoneNumber(e) { //点击获取手机号码按钮  
				this.encrypdata = e.detail.encryptedData;
				this.ivdata = e.detail.iv;
				this.$request.get({
					url: phoneUrl,
					data: {
						encrypdata: this.encrypdata,
						ivdata: this.ivdata,
						sessionkey: this.sessionKey
					},
				}).then(res => {
					let phoneNumber = res;
					this.autoLogin(phoneNumber);
				})
			},
			//获取未读信息
			getMsgMap() {
				this.$request.get({
					url: 'sysMessage/getMsgMap',
				}).then(info => {
					console.log('info', info)
					for (let k of info) {
						if (k.label == '通知') {
							this.sysNum = k.value?k.value:0
						}
						if (k.label == '评论') {
							this.commitNum = k.value?k.value:0
						}
					}
					//显示小圆点
					if(parseFloat(this.sysNum)!==0||parseFloat(this.commitNum)!==0){
					      uni.showTabBarRedDot({index:2});
					}
				}, res => {});
			},
			//清空权限信息
			clearPermission(){
				this.clearPermList();
				uni.setStorageSync('permList','')
			},
			//自动登录注册
			autoLogin(phoneNumber) {
				let _self = this;
				this.$request.post({
					url: 'user/miniAutoLogin',
					data: {
						phoneNumber
					},
				}).then(res => {
					this.$request.get({
						url:'userOrgRelation/getMyAccountNew',
						header:{
							token:res.userInfo.token
						}
					}).then(v=>{
						if(v.length===1){
							// this.saveUserInfo(res.userInfo);
							// uni.setStorageSync("openid", this.openid);
							// this.SetIsSubscribe(true)
							// uni.setStorageSync('autoGoLoginFlag', false);
							// //判断是否显示未读消息红点
							// this.getMsgMap();
							// //静默登录
							// getApp().connectWS();
							
							// if (this.errorLonginFlag) {
							// 	uni.switchTab({
							// 		url: '/pages/home/home'
							// 	});
							// } else {
							// 	uni.navigateBack({});
							// }
							this.clearPermission();
							let  params = {};
							params.userInfoId = v[0].userInfoId;
							params.orgInfoId = v[0].orgInfoId;
							params.kcOpenId = this.openid;
							this.$request.post({
								url:'/userOrgRelation/setMyAccount',
								data:params,
								header:{
									token:res.userInfo.token
								}
							}).then(result=>{
								let userInfo = result.userInfo;
								let mainAccount = result.mainAccount;
								userInfo.token = res.userInfo.token;
								uni.setStorageSync('userInfo', userInfo);
								uni.setStorageSync('mainAccount',mainAccount);
								this.setMainAccount(mainAccount)
								this.saveUserInfo(userInfo);
								uni.setStorageSync('autoGoLoginFlag', false);				
								let empt = {};
								result.permList.map((d)=>{
									empt[d.perms]= true;
								})
								uni.setStorageSync('permList',empt)
								this.setPermList(empt)
								this.SetIsSubscribe(true)
								//判断是否显示未读消息红点
								this.getMsgMap();
								getApp().connectWS();
								uni.switchTab({
								    url: '/pages/myinfo/myinfo'
								});
							})
							
						}else {
							this.setToken(res.userInfo.token)
							uni.navigateTo({
								url:`/pagesA/select-account/select-account?openid=${this.openid}`
							})
						}
					})
					

				})
			}
		},

	}
</script>

<style scoped>
	.login-login-contain .login-logo {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 186upx;
	}

	.login-login-contain .login-img {
		width: 150upx;
		height: 102upx;
		margin-bottom: 15upx;
	}

	.login-login-contain .login-logo-text {
		font-size: 36upx;
		color: rgba(34, 34, 34, 1);
		margin-top: 6upx;
	}

	.login-login-contain .login-opt {
		margin-top: 206upx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.login-login-contain .login-opt-btn {
		width: 670upx;
		height: 88upx;
		background-color: #2A7DFA;
		color: white;
		border-radius: 8upx;
		font-size: 36upx;
		color: rgba(255, 255, 255, 1);
	}

	.login-login-contain .login-opt-text {
		font-size: 32upx;
		color: #3467C4;
		margin-top: 33upx;
	}

	.login-login-contain .login-pro-text {
		color: #9B9B9B;
		font-size: 26upx;
		/* font-family: PingFang-SC-Medium, PingFang-SC; */
		/* font-weight: 500; */
	}

	.login-login-contain .login-pro-pro {
		font-size: 26upx;
		/* font-family: PingFang-SC-Medium, PingFang-SC; */
		/* font-weight: 500; */
		color: #3467C4;
	}

	.login-login-contain .login-pro-con {
		position: absolute;
		width: 100%;
		left: 0;
		bottom: 56upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
